package com.teaphy.readlist.controller;

import com.teaphy.readlist.model.ServiceSetting;
import com.teaphy.readlist.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author teaphy
 * @desc
 * @since 10:27
 */
@RestController
public class UserController {
    Map<String, Object> params = new HashMap<>();

    // 从路径中获取参数
    @GetMapping(path = "/{city_id}/{user_id}")
    public Object getUser(@PathVariable String city_id, @PathVariable String user_id) {
        params.clear();
        params.put("cityId", city_id);
        params.put("userId", user_id);
        return params;
    }

    @GetMapping(path = "/v1/getUsers")
    public Object getUsers(@RequestParam(name = "page", defaultValue = "0")int from, int num) {
        params.clear();
        params.put("page", from);
        params.put("num", num);
        return params;
    }

    @GetMapping(path = "/v1/getUserBody")
    public Object getUserBody(@RequestBody User user) {
        params.clear();
        params.put("user", user);
        return params;
    }


    /**
     * 获取Header
     */
    @GetMapping(path = "/v1/getHeader/{id}")
    public Object getHeader(@PathVariable String id, @RequestHeader(name = "access_token") String token) {
        params.clear();
        params.put("id", id);
        params.put("token", token);
        return params;
    }


    /**
     * 自动注入获取参数
     * @param request
     * @return
     */
    @GetMapping(path = "/v1/testRequest")
    public Object testRequest(HttpServletRequest request) {
        params.clear();
        params.put("id", request.getParameter("id"));
        params.put("token", request.getHeader("access_token"));
        return params;
    }

    @PostMapping("/api/v1/login")
    public Object login(int id, String pwd) {
        params.clear();
        params.put("id", id);
        params.put("pwd", pwd);
        params.put("aaa", 2222);
        params.put("bbbb", 333333);
        return params;
    }

    @Autowired
    private ServiceSetting serviceSetting;

//    @GetMapping("/testProperties")
//    public Object testProperties() {
//        return serviceSetting;
//    }
}
